﻿using System;
using System.Collections.Generic;
using System.Text;
using static FySystem.Extension.DoubleExtension;

namespace FySystem.Extension
{
    public static class DecimalExtension
    {
        /// <summary>
        /// 单位转换
        /// </summary>
        /// <param name="val"></param>
        /// <param name="round"></param>
        /// <param name="unit"></param>
        /// <returns></returns>
        public static DecimalWithUnit ConvertUnits(this decimal? val, int round, EUnits unit, string baseUnit)
        {
            DecimalWithUnit res = new DecimalWithUnit();
            try
            {
                val = val == null ? 0 : val;
                res = val.Value.ConvertUnits(round, unit, baseUnit);
            }
            catch (Exception)
            {
                throw;
            }
            return res;
        }

        /// <summary>
        /// 单位转换
        /// </summary>
        /// <param name="val"></param>
        /// <param name="round"></param>
        /// <param name="unit"></param>
        /// <param name="baseUnit"></param>
        /// <returns></returns>
        public static DecimalWithUnit ConvertUnits(this decimal val, int round, EUnits unit, string baseUnit)
        {
            DecimalWithUnit res = new DecimalWithUnit();
            try
            {
                decimal tmp = Math.Abs(val);
                int intVal = (int)unit;

                while (tmp >= 1000)
                {
                    tmp = tmp / 1000;
                    intVal += 1;
                }

                if (intVal == 0)
                    res.cvalue[1] = baseUnit;
                else
                    res.cvalue[1] = ((EUnits)intVal).ToString() + baseUnit;

                if (round == -1)
                    res.cvalue[0] = tmp.ToString();
                else
                    res.cvalue[0] = Math.Round(tmp, round, MidpointRounding.AwayFromZero).ToString();

                if (val < 0)
                    res.cvalue[0] = $"-{res.cvalue[0]}";

                res.orivalue = val;
            }
            catch (Exception)
            {
                throw;
            }
            return res;
        }

        /// <summary>
        /// 相当于Oracle的NVL函数
        /// </summary>
        /// <param name="source"></param>
        /// <param name="dest"></param>
        /// <returns></returns>
        public static decimal NVL(this decimal? source, decimal dest = 0)
        {
            return source == null ? dest : source.Value;
        }

        /// <summary>
        /// 计算碳抵消，单位：吨
        /// </summary>
        /// <param name="energy"></param>
        /// <returns></returns>
        public static decimal CalCO2(this decimal energy, int round = 2)
        {
            return Math.Round(energy * 0.997m / 1000, round, MidpointRounding.AwayFromZero);
        }

        /// <summary>
        /// 计算减少了多少树砍伐，虽然公式看着很假，但网上查过，就是这么算的
        /// </summary>
        /// <param name="energy"></param>
        /// <returns></returns>
        public static decimal CalTrees(this decimal energy, int round = 2)
        {
            return Math.Round(energy / 365, round, MidpointRounding.AwayFromZero);
        }
    }

    /// <summary>
    /// 单位转换后的结果
    /// </summary>
    public class DecimalWithUnit
    {
        public decimal orivalue { get; set; }

        public string[] cvalue { get; set; } = new string[2];

        public static DecimalWithUnit GetZeroValue(string unit)
        {
            DecimalWithUnit res = new DecimalWithUnit();
            res.orivalue = 0;
            res.cvalue[0] = "0";
            res.cvalue[1] = unit;
            return res;
        }
    }
}
